//
//  PreparaViewController.m
//  wedding
//
//  Created by 常宽 on 15/1/15.
//  Copyright (c) 2015年 常宽. All rights reserved.
//

#import "PreparaViewController.h"
#import "RACollectionViewReorderableTripletLayout.h"    // 第三方自定义layout
#import "PreparationsCollectionCell.h"  // 自定义cell
#import "PreparationsCatalogue.h"       // cell里自定义的视图
#import "PCDetailsViewController.h"     // 二级页面
#import "PreparationsModel.h"           // model类(自定义的属性)

#define LabelHeight 7

@interface PreparaViewController ()
/// 主要布局的集合视图
@property (nonatomic, retain) UICollectionView *collectionView;
/// 目录
@property (nonatomic, retain) NSMutableArray *Catalogue; // 目录
@end

@implementation PreparaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, self.view.frame.size.width - 10, LabelHeight)];
    label.backgroundColor = [WebColor lavenderBlush];
    label.text = @"您知道吗~长按可排序~";
    label.font = [UIFont boldSystemFontOfSize:10];
    label.textColor = [WebColor magenta];
    [self.view addSubview:label];
    
    RACollectionViewReorderableTripletLayout *layout = [[RACollectionViewReorderableTripletLayout alloc]init];
    
    // navBar = 64, tabBar = 44,间距上下加起来10,label = LabelHeight
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, LabelHeight + 10, self.view.frame.size.width, self.view.frame.size.height - 64 - 44 - LabelHeight - 15) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [WebColor lightPink];
    // 注册cell
    [self.collectionView registerClass:[PreparationsCollectionCell class] forCellWithReuseIdentifier:@"Preparations"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self setupPhotosArray];
    
    [self.view addSubview:self.collectionView];
    
}

- (void)setupPhotosArray
{
//做本地存储, 判断数组是否有缓存
    // 获取caches文件夹, 用来保存缓存数据
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [cachesPath stringByAppendingPathComponent:@"PreparaArray.xml"];
    
    // 读取数据
    NSArray *dataRead = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    [_Catalogue removeAllObjects];
    _Catalogue = nil;
    if (dataRead != nil) {
        self.Catalogue = [NSMutableArray arrayWithArray:dataRead];
    } else {
        self.Catalogue = [[NSMutableArray alloc] init];
        NSArray *imageNames = [NSArray arrayWithObjects:@"精品推荐.png", @"理财.png", @"日历.png", @"收藏.png", @"说说.png", @"指南.png", nil];
        NSArray *cataNames = [NSArray arrayWithObjects:@"美品汇", @"记勒个帐", @"吉日", @"收藏", @"故事", @"攻略", nil];
        for (int i = 0; i < imageNames.count; i ++) {
            PreparationsModel *model = [[PreparationsModel alloc] init];
            model.title = [cataNames objectAtIndex:i];
            model.typeId = [NSNumber numberWithInt:i + 1080];
            model.imageName = [imageNames objectAtIndex:i];
            
            [_Catalogue addObject:model];
        }
    }
    
}

#pragma mark collection自定义及系统的协议方法
//  返回section数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//  返回每块section中cell数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//#warning 1
    return _Catalogue.count;
}
//  section间距
- (CGFloat)sectionSpacingForCollectionView:(UICollectionView *)collectionView
{
    return 5.f;
}
//  item间距
- (CGFloat)minimumInteritemSpacingForCollectionView:(UICollectionView *)collectionView
{
    return 5.f;
}
//  线间距
- (CGFloat)minimumLineSpacingForCollectionView:(UICollectionView *)collectionView
{
    return 5.f;
}
// inset(插图?)距离集合视图
- (UIEdgeInsets)insetsForCollectionView:(UICollectionView *)collectionView
{
    return UIEdgeInsetsMake(5.f, 0, 5.f, 0);
}
// items在section中的大小?
- (CGSize)collectionView:(UICollectionView *)collectionView sizeForLargeItemsInSection:(NSInteger)section
{
    return RACollectionViewTripletLayoutStyleSquare; //same as default !
}
// 自动滚动边缘?
- (UIEdgeInsets)autoScrollTrigerEdgeInsets:(UICollectionView *)collectionView
{
    return UIEdgeInsetsMake(50.f, 0, 50.f, 0); //Sorry, horizontal scroll is not supported now.
}
// 自动滚动触发填补
- (UIEdgeInsets)autoScrollTrigerPadding:(UICollectionView *)collectionView
{
    return UIEdgeInsetsMake(64.f, 0, 0, 0);
}
// 重新排序item的alpha
- (CGFloat)reorderingItemAlpha:(UICollectionView *)collectionview
{
    return .3f;
}
// 结束拖拽
- (void)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout didEndDraggingItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 获取caches文件夹, 用来保存缓存数据
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [cachesPath stringByAppendingPathComponent:@"PreparaArray.xml"];
    // 利用归档类 将data写入本地
    NSArray *data = [NSArray arrayWithArray:_Catalogue];
    BOOL result = [NSKeyedArchiver archiveRootObject:data toFile:filePath];
    NSLog(@"数组写入结果: %d", result);
    [self.collectionView reloadData];
}
// item从什么位置移动到什么位置
- (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath didMoveToIndexPath:(NSIndexPath *)toIndexPath
{
    PreparationsModel *str = [_Catalogue objectAtIndex:fromIndexPath.item];
    [_Catalogue removeObjectAtIndex:fromIndexPath.item];
    [_Catalogue insertObject:str atIndex:toIndexPath.item];

}
// item能从什么位置移动到什么位置,返回是否可以移动
- (BOOL)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSIndexPath *)fromIndexPath canMoveToIndexPath:(NSIndexPath *)toIndexPath
{
    return YES;
}
// item是否可以移动
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
// 系统的返回CELL
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"Preparations";
    PreparationsCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    [cell.catalogueView removeFromSuperview];
    cell.catalogueView.frame = cell.bounds;
    cell.catalogueView.backgroundColor = [WebColor crimson];
    
    PreparationsModel *model = _Catalogue[indexPath.item];
    
    cell.catalogueView.tuBiao.image = [UIImage imageNamed:model.imageName];
    cell.catalogueView.title.text = model.title;
    [cell.contentView addSubview:cell.catalogueView];
    
    return cell;
    
}
/// 选中时的
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    PCDetailsViewController *pcdController = [[PCDetailsViewController alloc] init];
    
    pcdController.model = _Catalogue[indexPath.item];
    
    [self.navigationController pushViewController:pcdController animated:YES];
    
}


#pragma mark -


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
